package com.ericsson.enap.modules.autoCheck.job;

import com.ericsson.common.quartz.job.MainJob;
import com.ericsson.common.quartz.job.ManualQuartzJob;
import com.ericsson.common.quartz.service.QuartzTableManager;
import com.ericsson.common.util.SpringUtils;
import com.ericsson.enap.modules.autoCheck.service.DictEopsService;
import com.ericsson.enap.modules.autoCheck.task.AutoCheckScheduleTask;
import com.ericsson.enap.modules.autoCheck.task.EopsTemplateScheduleTask;
import com.ericsson.epc.modules.sys.entity.Dict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @description: 新版自动巡检job
 * @author: chenhongbo
 * @create: 2021-11-11 11:02
 */
@Component
public class AutoCheckScheduleJob implements MainJob {
    @Autowired
    QuartzTableManager quartzTableManager;

    @Autowired
    private DictEopsService dictService;

    @Override
    public void doTask() {
        List<Dict> intervals = dictService.getDictList("enip_autocheck_interval");
        for(Dict interval : intervals){
            AutoCheckScheduleTask eopsTask = SpringUtils.getBean("autoCheckScheduleTask");
            eopsTask.setInterval(interval.getValue());
            eopsTask.setTemplateTypes("3,4");  //只查询巡检类型的模板
            //eopsTask.execute();
            quartzTableManager.addManualTaskJob("AutoCheckSchedule" + interval.getValue(),
                    "AutoCheckSchedule", "AutoCheckSchedule" + interval.getValue(),
                    "AutoCheckSchedule", ManualQuartzJob.class, getCronByInterval(interval.getValue()),eopsTask);
        }
    }

    private static String getCronByInterval(String interval){
        switch (interval){
            case "1":return "0 0/1 * * * ?";
            case "5":return "0 0/5 * * * ?";
            case "30":return "0 0/30 * * * ?";
            case "60":return "0 0 0/1 * * ?";
            case "1440":return "0 0 0 * * ?";
            case "10080":return "0 0 0 ? * 1";
            default:return "0 0/15 * * * ?";
        }

    }
}
